package com.hongguan.oms.service.meeting;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.hongguan.oms.service.BaseService;
import com.hongguan.oms.utils.Util;

/**
 * @author <a href="mailto:jiangting@hongguaninfo.com">jiangting</a>
 */
@SuppressWarnings("unchecked")
@Service
public class MeetingInfoService extends BaseService {
	public MeetingInfoService() {
		this.namespace = "T_MEETING_INFO";
	}
	/**
	 * search meeting info 
	 * 
	 * @param param
	 * @return
	 */
	public List searchMeeting(Map param) {
		return this.getSqlMapClientTemplate().queryForList(
				this.getStatementName("meeting_search"), param);
	}
	/**
	 * add a meeting
	 */
	public void addMeeting(Map param, HttpServletRequest request) {
		long id = (Long) this.getSqlMapClientTemplate().insert(
				this.getStatementName("meeting_add"), param);
		this.addMeetingJoin(request, id);
	}

	/**
	 * add meeting jioner
	 */
	public void addMeetingJoin(Map param) {
		this.getSqlMapClientTemplate().insert(
				this.getStatementName("meeting_join_add"), param);
	}

	/**
	 * add meeting jioner
	 */
	public void addMeetingJoin(HttpServletRequest request, Long meetingId) {
		Map param = new HashMap();
		String mt_joiner = Util.toNull(request.getParameter("userId"));
		String[] mt_joiners = mt_joiner.split(";");
		for (int i = 0; i < mt_joiners.length; i++) {
			String join = mt_joiners[i];
			param.put("mt_id", meetingId);
			param.put("user_id", join);
			param.put("mtj_status", 0);
			this.addMeetingJoin(param);
		}
	}

	/**
	 * search meeting by meeting id
	 */
	public HashMap searchMeetingById(Map param) {
		return (HashMap) this.getSqlMapClientTemplate().queryForObject(
				this.getStatementName("meeting_search_byid"), param);
	}

	/**
	 * search meeting by datetime
	 */
	public List searchMeetingByDate(Map param) {
		return (List) this.getSqlMapClientTemplate().queryForList(
				this.getStatementName("meeting_search_byDate"), param);
	}

	/**
	 * search meeting by datetime
	 */
	public long searchMeetingDateValidate(Map param) {
		return (Long) this.getSqlMapClientTemplate().queryForObject(
				this.getStatementName("meeting_date_validate"), param);
	}

	/**
	 * edit meeting info
	 */
	public void editMeeting(Map param) {
		this.getSqlMapClientTemplate().update(
				this.getStatementName("meeting_edit"), param);
	}

	/**
	 * edit meeting joiner
	 */
	public void editMeetingJoin(HttpServletRequest request, Long meetingId) {
		Map param = new HashMap();
		String mt_joiner = Util.toNull(request.getParameter("userId"));
		String[] mt_joiners = mt_joiner.split(";");
		param.put("mt_id", meetingId);
		param.put("mtj_status", 1);
		this.editMeetingJoin(param);
		List list = this.searchMeetingJoin(param);
		for (int i = 0; i < mt_joiners.length; i++) {
			param.put("mtj_status", 0);
			int oldJoin = Integer.parseInt(mt_joiners[i]);
			for (int j = 0; j < list.size(); j++) {
				int newJoin = (Integer) list.get(j);
				param.put("user_id", newJoin);
				if (oldJoin == newJoin) {
					editMeetingJoin(param);
				} else if (oldJoin != newJoin) {
					addMeetingJoin(param);
				}
			}
			this.addMeetingJoin(param);
		}
	}

	/**
	 * search meeting jioner by meeting id
	 */
	public List searchMeetingJoin(Map param) {
		return (List) this.getSqlMapClientTemplate().queryForList(
				this.getStatementName("meetingJoin_search_byMtId"), param);
	}

	/**
	 * update jioner's stauts by meeting id
	 */
	public void editMeetingJoin(Map param) {
		this.getSqlMapClientTemplate().update(
				this.getStatementName("edit_meetingJoin_byMtId"), param);
	}
}
